package cn.gjculture.shopping.api.controller;

import cn.gjculture.shopping.api.dao.CommonDeleteMapper;
import cn.gjculture.shopping.api.entity.*;
import cn.gjculture.shopping.api.service.GoodsTypeService;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/goodstype")
public class GoodsTypeController {

    @Autowired
    GoodsTypeService goodsTypeService;

    @Autowired
    CommonDeleteMapper commonDao;

    @ApiOperation(value="保存商品类型", notes="保存商品类型")
    @ApiImplicitParam(name = "goodsType", value = "商品类型", required = true, dataType = "Object")
    @PostMapping
    public Return save(String param){
        ParamData<GoodsType> g = JSONObject.parseObject(param, new TypeReference<ParamData<GoodsType>>() {});
        Return ret=new Return();
        goodsTypeService.insert(g.getData());
        ret.getCommon().setRet(true);
        return ret;
    }

    @ApiOperation(value="修改商品类型", notes="修改商品类型")
    @ApiImplicitParam(name = "goods", value = "商品对象对象", required = true, dataType = "Object")
    @PutMapping
    public Return update(String param){
        ParamData<GoodsType> g = JSONObject.parseObject(param, new TypeReference<ParamData<GoodsType>>() {});
        Return ret=new Return();
        if( goodsTypeService.update(g.getData())>0){
            ret.getCommon().setRet(true);
        }
        return ret;
    }

    @ApiOperation(value="获取商品类型列表", notes="获取商品类型列表")
    @GetMapping
    public Return<List<GoodsType>> query(PageInfo pageInfo){
        Return<List<GoodsType>> ret=new Return<>();
        List<GoodsType> types=goodsTypeService.query();
        ret.setData(types);
        return ret;
    }
    @ApiOperation(value="删除商品类型", notes="删除商品类型")
    @ApiImplicitParam(name = "goodsTypeIds", value = "商品类型ID", required = true, dataType = "String", paramType = "path")
    @DeleteMapping(value = "/{goodsTypeIds}")
    public Return deleteById(@PathVariable(value = "goodsTypeIds") List<String> goodsTypeIds){
        Return ret=new Return<>();
        if(goodsTypeService.deleteMore(goodsTypeIds,ret)>0)
        ret.getCommon().setRet(true);
        return ret;
    }
}
